Imports ccapps.persistent.CaseTracker

Public Class CaseDefinition
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblContextHeader As System.Web.UI.WebControls.Label
    Protected WithEvents ContentPaneFooter1 As Library.WebControls.ContentPaneFooter
    Protected WithEvents ddlType As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtEstimatedTime As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDesciption As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtReason As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtProductionDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtRelease As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtNotes As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator4 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator5 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents fuAttachment As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents txtArea As System.Web.UI.WebControls.TextBox
    Protected WithEvents rfvAttachment As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtTemp As System.Web.UI.WebControls.TextBox
    Protected WithEvents Lblok As System.Web.UI.WebControls.Label
    Protected WithEvents rfvArea As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents Requiredfieldvalidator6 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents lblCaseItemID As System.Web.UI.WebControls.Label
    Protected WithEvents txtCaseItemID As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddlGroup As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.txtTemp.Style("display") = "none"

        If Not persistent.CaseTracker.IsUserAuthorized(Session("user"), AuthorizationLevel.ViewAndCreate) Then
            Page.Controls.Clear()
            Response.Write("You are not authorized to view this page!")
            Return
        End If


        Me.PageTitle = "DCR Case Definition"
        Me.lblContextHeader.Text = "Please fill-in DCR details and click Save."


        If Not Me.IsPostBack Then

            For Each item As ListItem In persistent.CaseTracker.GetListItemsFromEnum(GetType(persistent.CaseTracker.UserGroups))
                Me.ddlGroup.Items.Add(item)
            Next
            Me.ddlGroup.SelectedValue = persistent.CaseTracker.GetUser(Session("user"))("UserGroup")
            For Each itemTypeName As String In ItemType.GetNames(GetType(ItemType))
                'Me.ddlType.Items.Add(New ListItem(itemTypeName, [Enum].Parse(GetType(ItemType), itemTypeName)))
                Me.ddlType.Items.Add(itemTypeName)
            Next

            For Each item As ListItem In persistent.CaseTracker.GetListItemsFromEnum(GetType(persistent.CaseTracker.CaseStates))
                Me.ddlStatus.Items.Add(item)
            Next

        End If
    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        Dim attachmentsPath As String = Server.MapPath("~/CaseTrackerAttachments/")
        Dim fileInfo As IO.FileInfo
        Dim fileNameQualifier As Integer = 0
        Dim savedFileName As String = String.Empty

        If Not (Me.fuAttachment.PostedFile Is Nothing OrElse Me.fuAttachment.PostedFile.FileName = String.Empty) Then
            fileInfo = New IO.FileInfo(attachmentsPath & Me.fuAttachment.PostedFile.FileName.Substring(Me.fuAttachment.PostedFile.FileName.LastIndexOf("\") + 1))
            Dim fileName As String = Me.fuAttachment.PostedFile.FileName.Substring(Me.fuAttachment.PostedFile.FileName.LastIndexOf("\") + 1).Replace(fileInfo.Extension, "")
            While fileInfo.Exists
                fileNameQualifier += 1
                fileInfo = New IO.FileInfo(attachmentsPath & fileName & "[" & fileNameQualifier.ToString() & "]" & fileInfo.Extension)
            End While

            Me.fuAttachment.PostedFile.SaveAs(fileInfo.FullName)
            savedFileName = fileInfo.Name
        End If

        Try
            Dim caseID As Integer = persistent.CaseTracker.AddCase(Me.ddlType.SelectedValue, Me.txtCaseItemID.Text, Me.txtArea.Text, Me.txtEstimatedTime.Text, Me.txtDesciption.Text, Me.txtReason.Text, Me.txtProductionDate.Text, Me.txtRelease.Text, savedFileName, Me.txtNotes.Text, Session("user"))
                If Not (persistent.CaseTracker.IsCaseExists(caseID) AndAlso persistent.CaseTracker.IsCaseHistoryExists(caseID, 1)) Then
                    persistent.CaseTracker.DeleteCase(caseID)
                    Throw New Exception("Case creation failed!")
                End If
            Me.Lblok.Text = "Case is created."
                Me.ddlType.SelectedIndex = 0
                Me.txtCaseItemID.Text = ""
                Me.txtArea.Text = ""
                Me.txtEstimatedTime.Text = ""
                Me.txtDesciption.Text = ""
                Me.ddlStatus.SelectedIndex = 0
                Me.txtReason.Text = ""
                Me.txtProductionDate.Text = ""
                Me.txtRelease.Text = ""
                Me.txtNotes.Text = ""
        Catch ex As Exception
            IO.File.Delete(fileInfo.FullName)
            Me.Lblok.Text = "Exception occured : " & ex.Message
        End Try

    End Sub
End Class
